﻿//
// *************************************************************************************
// WP-Framework
// 
// Developed by
// Lukas Kretschmar (lkretschmar.dev@bluewin.ch)
//
// Switzerland, Copyright (c) 2013 lkretschmar
// This content is released under the MIT License (http://opensource.org/licenses/MIT).
//     
// *************************************************************************************
//
//
			

using System.Windows.Input;
using KretschIT.WP_Fx.Core.Validation;
using System;
namespace KretschIT.WP_Fx.UI.Commands
{
    public class RelayCommandAdapter : IRelayCommand
    {
        private ICommand command;

        #region Ctor

        public RelayCommandAdapter(ICommand command)
        {
            It.IsNotNull(() => command);
            this.command = command;
            this.WireCanExecuteChanged();
        }

        private void WireCanExecuteChanged()
        {
            this.command.CanExecuteChanged += (sender, args) => this.RaiseCanExecuteChangedEvent(sender, args);
        }

        #endregion

        #region CanExecute

        public void RaiseCanExecuteChanged()
        {
            this.RaiseCanExecuteChangedEvent();
        }

        private void RaiseCanExecuteChangedEvent(object sender = null, EventArgs args = null)
        {
            if (this.CanExecuteChanged != null)
            {
                this.CanExecuteChanged(sender ?? this.command, args ?? EventArgs.Empty);
            }
        }

        public event EventHandler CanExecuteChanged;

        public bool CanExecute(object parameter)
        {
            return this.command.CanExecute(parameter);
        }

        #endregion

        #region Execute

        public void Execute(object parameter)
        {
            this.command.Execute(parameter);
        }

        #endregion
    }
}
